home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / Arashi 1.1 / RightWriter / RightWrite.c next >
C/C++ Source or Header  |  1992-08-25  |  5KB  |  213 lines

  1. /*/
  2.      Project Arashi/STORM: RightWrite.c
  3.      Major release: 9/9/92
  4.  
  5.      Last modification: Tuesday, August 25, 1992, 22:38
  6.      Created: Tuesday, August 25, 1992, 14:58
  7.  
  8.      Copyright © 1992, Juri Munkki
  9. /*/
  10.  
  11. #include <Packages.h>
  12.  
  13. Handle    StartText,EndText,ContentText;
  14. int        StartLen,EndLen;
  15.  
  16. void    DoMungersToComment(FilePB,Content)
  17. FileParam    *FilePB;
  18. Handle        Content;
  19. {
  20.     char            Keyword[256];
  21.     char            TimeString[256];
  22.     DateTimeRec        CreateDate,ModifyDate;
  23.     
  24.     GetIndString(Keyword,128,1);
  25.     Munger(Content,0,Keyword+1,*Keyword,FilePB->ioNamePtr+1,*(FilePB->ioNamePtr));
  26.     Secs2Date(FilePB->ioFlCrDat,&CreateDate);
  27.     Secs2Date(FilePB->ioFlMdDat,&ModifyDate);
  28.     
  29.     NumToString(CreateDate.year,TimeString);
  30.     GetIndString(Keyword,128,3);
  31.     Munger(Content,0,Keyword+1,*Keyword,TimeString+1,*TimeString);
  32.  
  33.     if(CreateDate.year != ModifyDate.year)
  34.     {    int    firstlen;
  35.     
  36.         firstlen = TimeString[0];
  37.         NumToString(ModifyDate.year,firstlen+1+TimeString);
  38.         TimeString[0]+= 1+TimeString[firstlen+1];
  39.         TimeString[firstlen+1] = '-';
  40.     }
  41.  
  42.     GetIndString(Keyword,128,2);
  43.     Munger(Content,0,Keyword+1,*Keyword,TimeString+1,*TimeString);
  44.  
  45.     NumToString(ModifyDate.year,TimeString);
  46.     GetIndString(Keyword,128,4);
  47.     Munger(Content,0,Keyword+1,*Keyword,TimeString+1,*TimeString);
  48.  
  49.     IUDateString(FilePB->ioFlCrDat,longDate,(void *)TimeString);
  50.     GetIndString(Keyword,128,5);
  51.     Munger(Content,0,Keyword+1,*Keyword,TimeString+1,*TimeString);
  52.  
  53.     IUDateString(FilePB->ioFlMdDat,longDate,(void *)TimeString);
  54.     GetIndString(Keyword,128,6);
  55.     Munger(Content,0,Keyword+1,*Keyword,TimeString+1,*TimeString);
  56.  
  57.     IUTimeString(FilePB->ioFlCrDat,FALSE,(void *)TimeString);
  58.     GetIndString(Keyword,128,7);
  59.     Munger(Content,0,Keyword+1,*Keyword,TimeString+1,*TimeString);
  60.  
  61.     IUTimeString(FilePB->ioFlMdDat,FALSE,(void *)TimeString);
  62.     GetIndString(Keyword,128,8);
  63.     Munger(Content,0,Keyword+1,*Keyword,TimeString+1,*TimeString);
  64. }
  65.  
  66. void    DoFileOperations()
  67. {
  68.     int            message,count,i;
  69.     AppFile        thefile;
  70.     FileParam    block;
  71.     
  72.     CountAppFiles(&message,&count);
  73.     if(message==1)
  74.     {    return;
  75.     }
  76.     else
  77.     {    for(i=1;i<=count;i++)
  78.         {    int            ref;
  79.         
  80.             GetAppFiles(i,&thefile);
  81.  
  82.             block.ioCompletion=0;
  83.             block.ioNamePtr=thefile.fName;
  84.             block.ioVRefNum=thefile.vRefNum;
  85.             block.ioFVersNum=thefile.versNum;
  86.             block.ioFDirIndex=0;
  87.             PBGetFInfo(&block,0);
  88.  
  89.             if(FSOpen(thefile.fName,thefile.vRefNum,&ref)==0)
  90.             {    Handle        FileContent;
  91.                 long        FileSize;
  92.  
  93.                 GetEOF(ref,&FileSize);
  94.                 FileContent = NewHandle(FileSize);
  95.                 if(FileContent)
  96.                 {    HLock(FileContent);
  97.                     if(FSRead(ref,&FileSize,*FileContent)==0)
  98.                     {    long    firstPlace,secondPlace;
  99.                         
  100.                         HUnlock(FileContent);
  101.                         firstPlace = Munger(FileContent,0,*StartText,StartLen,0,0);
  102.                         if(firstPlace>=0)
  103.                         {    secondPlace = Munger(FileContent,
  104.                                                     firstPlace+StartLen,
  105.                                                     *EndText,EndLen,0,0);
  106.                             if(secondPlace>=0)
  107.                             {    int        Failure = FALSE;
  108.                                 Handle    NewContent;
  109.                                 long    WriteLen;
  110.                             
  111.                                 NewContent = ContentText;
  112.                                 HandToHand(&NewContent);
  113.                                 DoMungersToComment(&block,NewContent);
  114.                                 HLock(NewContent);
  115.                                 
  116.                                 SetEOF(ref,0);
  117.                                 
  118.                                 WriteLen = firstPlace;
  119.                                 Failure |= 0 != FSWrite(ref,&WriteLen,*FileContent);
  120.  
  121.                                 WriteLen = GetHandleSize(NewContent);
  122.                                 Failure |= 0 != FSWrite(ref,&WriteLen,*NewContent);
  123.  
  124.                                 WriteLen = FileSize-secondPlace-EndLen;
  125.                                 Failure |= 0 != FSWrite(ref,&WriteLen,
  126.                                                         *FileContent+secondPlace+EndLen);
  127.                                 
  128.                                 if(Failure)
  129.                                 {    SysBeep(10);
  130.                                     SetEOF(ref,0);
  131.                                     FSWrite(ref,&FileSize,*FileContent);
  132.                                 }
  133.                                 
  134.                                 HUnlock(NewContent);
  135.                                 DisposHandle(NewContent);        
  136.                             }
  137.                         }
  138.                         
  139.                     }
  140.                     else
  141.                     {    HUnlock(FileContent);
  142.                     }
  143.                     DisposHandle(FileContent);
  144.                 }            
  145.                 FSClose(ref);
  146.  
  147.             }
  148.  
  149.             PBSetFInfo(&block,0);
  150.             
  151.         }
  152.     }
  153. }
  154.  
  155. pascal    int        keyfilter(theDialog,theEvent,itemHit)
  156. DialogPtr    theDialog;
  157. EventRecord    *theEvent;
  158. int            *itemHit;
  159. {
  160.     if(theEvent->what==keyDown && (theEvent->modifiers & cmdKey))
  161.     {    switch((char)theEvent->message)
  162.         {    case 'Q':
  163.             case 'q':
  164.             case '.':
  165.                 *itemHit=2;
  166.                 break;
  167.             case 'D':
  168.             case 'd':
  169.             case ' ':
  170.             case 13:
  171.             case 3:
  172.                 *itemHit=1;
  173.                 break;
  174.             default:
  175.                 *itemHit=0;
  176.         }
  177.         return -1;
  178.     }
  179.     return 0;
  180. }
  181.  
  182. void    main()
  183. {
  184.     DialogPtr    myDialog;
  185.     short        item;
  186.     short        itemType;
  187.     Rect        itemBox;
  188.  
  189.     DoInits();
  190.     myDialog = GetNewDialog(128,0,(WindowPtr)-1);
  191.     
  192.     do
  193.     {    ModalDialog(keyfilter,&item);
  194.     }    while(item != 1 && item != 2);
  195.     
  196.     if(item == 1)
  197.     {    GetDItem(myDialog,6,&itemType,&StartText,&itemBox);
  198.         HandToHand(&StartText);
  199.         HLock(StartText);
  200.         
  201.         StartLen = GetHandleSize(StartText);
  202.         GetDItem(myDialog,7,&itemType,&EndText,&itemBox);
  203.         HandToHand(&EndText);
  204.         HLock(EndText);
  205.         
  206.         EndLen = GetHandleSize(EndText);
  207.         GetDItem(myDialog,8,&itemType,&ContentText,&itemBox);
  208.         HandToHand(&ContentText);
  209.         
  210.         DoFileOperations();
  211.     }
  212.     DisposDialog(myDialog);
  213. }